$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/oem/typelist',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'loanTypeId', index: 'loanTypeId', width: 50 ,key:true,hidden:true},
			{ label: '名称', name: 'loanTypeName', index: 'loanTypeName'},
			// { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
			{ label: '图标'
				, name: 'loanTypeIcon', index: 'loanTypeIcon',formatter: function(value, options, row){
				return '<img src="'+value+'" width="30" height="30" />';
			}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,

		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        loadComplete:function (data) {
			console.log("data",window.parent.permissions);
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $("#loanIconImage").click(function () {
		$("#loanIconInput").click();
    });
    $("#loanIconInput").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#loanIconImage").attr("src", objUrl);
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
		title: null,
		isUpdate:false,
		loan: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.showInfo = true;
			vm.title = "新增";
			vm.loan = {};
            vm.isUpdate = false;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            // vm.app = $("#jqGrid").jqGrid().getRowData(appGradeId);
            vm.showList = false;
            vm.showInfo = true;
            vm.title = "修改";
            vm.getInfo(id);
            vm.isUpdate = true;
		},
        saveOrUpdate: function (event) {
			var urlStr = !vm.isUpdate ? "/platform/oem/typeadd" : "/platform/oem/typeupdate";
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.loan)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'loanIconInput',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.message);
                        if (this.lecture.id == null) {
                            $("#loanIconImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#loanIconInput").replaceWith('<input type="file" style="display: none;" accept="image/*" name="icon" id="loanIconInput" class="form-control" placeholder="file" />');
                    $("#loanIconInput").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#loanIconImage").attr("src", objUrl);
                        }
                    });
                },
                error: function (e) {
                    console.log("error", e);
                    $("#loanIconImage").attr("src", "");
                    alert("请求失败，请重试");
                }
            });
		},
		del: function (event) {
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
				    url: "/platform/oem/typedel/"+id,
				    success: function(r){
						if(r.code == 200){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("/platform/oem/typeinfo/"+id, function(r){
				if (r.code != 200){
				    alert(r.message);
                }
                vm.loan = r.result;
            });
		},
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});